package com.baseproject.mvp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baseproject.BaseFragment;
import com.baseproject.IRxSubscriberRefresh;
import com.baseproject.IRxSubscriberView;
import com.baseproject.appconfig.IActivityResultHandler;
import com.baseproject.mvp.delegate.FragmentMvpDelegate;
import com.baseproject.mvp.delegate.FragmentMvpDelegateImpl;
import com.baseproject.mvp.delegate.MvpDelegateCallback;
import com.baseproject.util.LogUtils;


public abstract class MvpFragment<V extends MvpFragment, P extends MvpBasePresenter<V>> extends BaseFragment
        implements MvpView, MvpDelegateCallback<V, P>, IRxSubscriberView, IActivityResultHandler {

//////////////////////IActivityResultHandler 基类默认实现 start ///////////////////
    @Override
    public void loginSuccess(int requestCode, int resultCode, Intent data) {

    }

    @Override
    public void activityBookReaderReturn(int requestCode, int resultCode, Intent data) {

    }

    @Override
    public void orderSuccess(int requestCode, int resultCode, Intent data) {

    }

    ////////////////////////////////////IActivityResultHandler 基类默认实现 end ////////////////////////////////

    @Override
    public String getPurpose() {
        return mJumpCallbackHelper.getPurpose();
    }

    @Override
    public Context getContext() {
        return super.getContext();
    }

    @Override
    public IRxSubscriberRefresh getRefresh() {
        return null;
    }

    public P mPresenter;

    protected FragmentMvpDelegate<V, P> mvpDelegate;

    /**
     * * Get the mvp delegate. This is internally used for creating presenter, attaching and
     * detaching view from presenter.
     *
     * <p>
     * <b>Please note that only one instance of mvp delegate should be used per fragment
     * instance</b>.
     * </p>
     *
     * <p>
     * Only override this method if you really know what you are doing.
     * </p>
     *
     * @return {@link FragmentMvpDelegateImpl}
     */
    @NonNull
    protected FragmentMvpDelegate<V, P> getMvpDelegate() {
        if (mvpDelegate == null) {
            mvpDelegate = new FragmentMvpDelegateImpl<>(this, this);
        }
        return mvpDelegate;
    }

    @Override
    public P getPresenter() {
        return mPresenter;
    }

    @Override
    public V getMvpView() {
        return (V) this;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (currentFragmentShowingOnTop()) {
            LogUtils.d("[onCreateView] really top this = " + this);
            getMvpDelegate().onCreateView(inflater, container, savedInstanceState);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        if (currentFragmentShowingOnTop()) {
            getMvpDelegate().onViewCreated(view, savedInstanceState);
//        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getMvpDelegate().onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getMvpDelegate().onDestroy();
    }

    public void setPresenter(P presenter) {
        this.mPresenter = presenter;
    }

}
